package com.kmzx.one.project.service.impl;

import com.kmzx.one.common.exceptions.NoSuchObjectException;
import com.kmzx.one.common.exceptions.ParameterNullException;
import com.kmzx.one.common.utils.UUIDGenerator;
import com.kmzx.one.project.entity.UserAddress;
import com.kmzx.one.project.entity.XZQHTable;
import com.kmzx.one.project.mapper.UserAddressMapper;
import com.kmzx.one.project.others.dto.UserAddressInfo;
import com.kmzx.one.project.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class UserAddressServiceImpl implements UserAddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;


    @Override
    public List<UserAddressInfo> findAllAddress(String userId) throws Exception {
        List<UserAddressInfo> list = userAddressMapper.findAllAddress(userId);
        if (list != null && list.size() > 0) {
            return list;
        } else {
            List<UserAddressInfo> objects = Collections.emptyList();
            return objects;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer userAddressAdd(UserAddressInfo info) throws Exception {
        UserAddress userAddress = new UserAddress();
        Date now = new Date();
        String code = info.getCode();
        userAddress.setId(UUIDGenerator.generate());
        userAddress.setProvince(info.getProvince());
        userAddress.setCity(info.getCity());
        userAddress.setTown(info.getTown());
        userAddress.setOthers(info.getOthers());
        userAddress.setMobile(info.getMobile());
        userAddress.setUserId(info.getUserId());
        userAddress.setAddressDefault(info.getIsDefault());
        userAddress.setCreateTime(now);
        userAddress.setUpdateTime(now);
        userAddress.setDeleted(0);
        userAddress.setPostal(info.getPostal());
        userAddress.setUserName(info.getUserName());
        userAddress.setXzqhCode(code);

        List<String> codes = Arrays.asList("540000", "650000", "710000", "810000", "820000", "900000");
        if (code != null && !codes.contains(code)) {
            code = "E";
        }
        if (code == null || "".equals(code)) {
            code = "E";
        }
        userAddress.setCode(code);

        int isDefault = info.getIsDefault();
        if (isDefault == 1) {
            List<UserAddressInfo> list = userAddressMapper.findAllAddress(info.getUserId());
            if (list != null && list.size() > 0) {
                int resetNum = userAddressMapper.resetUserDefaultAddress(info.getUserId());
                if (resetNum >= 0) {
                    int insert = userAddressMapper.insertAddress(userAddress);
                    return insert;
                } else {
                    int insert = userAddressMapper.insertAddress(userAddress);
                    return insert;
                }
            } else {
                int insert = userAddressMapper.insertAddress(userAddress);
                return insert;
            }
        } else {
            userAddress.setIsPriority(1);
            int insert = userAddressMapper.insertAddress(userAddress);
            return insert;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer userAddressEdit(UserAddressInfo info) throws Exception {
        UserAddress userAddress = userAddressMapper.selectById(info.getAddressId());
        if (userAddress == null) {
            throw new ParameterNullException("地址不存在");
        }
        Date now = new Date();
        int isDefault = info.getIsDefault();
        userAddress.setProvince(info.getProvince());
        userAddress.setCity(info.getCity());
        userAddress.setTown(info.getTown());
        userAddress.setMobile(info.getMobile());
        userAddress.setOthers(info.getOthers());
        userAddress.setUserId(info.getUserId());
        userAddress.setUserName(info.getUserName());
        userAddress.setPostal(info.getPostal());
        userAddress.setAddressDefault(isDefault);
        userAddress.setUpdateTime(now);

        String code = info.getCode();
        userAddress.setXzqhCode(code);
        List<String> codes = Arrays.asList("540000", "650000", "710000", "810000", "820000", "900000");
        if (code != null && !codes.contains(code)) {
            code = "E";
        }
        if (code == null || "".equals(code)) {
            code = "E";
        }
        userAddress.setCode(code);

        if (isDefault == 1) {
            List<UserAddressInfo> list = userAddressMapper.findAllAddress(info.getUserId());
            if (list != null && list.size() > 0) {
                int resetNum = userAddressMapper.resetUserDefaultAddress(info.getUserId());
                if (resetNum >= 0) {
                    int insert = userAddressMapper.updateByIdSelective(userAddress);
                    return insert;
                } else {
                    int insert = userAddressMapper.updateByIdSelective(userAddress);
                    return insert;
                }
            } else {
                int insert = userAddressMapper.updateByIdSelective(userAddress);
                return insert;
            }
        } else {
            int insert = userAddressMapper.updateByIdSelective(userAddress);
            return insert;
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delUserAddress(String userId, String addressId) throws Exception {
        UserAddress userAddress = userAddressMapper.selectById(addressId);
        if (userAddress == null) {
            throw new ParameterNullException("地址不存在");
        }
        Date now = new Date();
        userAddress.setDeleted(1);
        userAddress.setUpdateTime(now);
        int insert = userAddressMapper.updateByIdSelective(userAddress);
        return insert;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer setUserAddressDefault(String userId, String addressId) throws Exception {
        UserAddress userAddress = userAddressMapper.selectById(addressId);
        if (userAddress == null) {
            throw new ParameterNullException("该地址不存在");
        }
        if (userAddress.getDeleted() == 1) {
            throw new NoSuchObjectException("该地址不存在");
        }
        userAddressMapper.resetUserDefaultAddress(userId);
        userAddress.setAddressDefault(1);
        int insert = userAddressMapper.updateByIdSelective(userAddress);
        return insert;
    }

    @Override
    public int resetUserPriorityAddress(String userId) {
        return userAddressMapper.resetUserPriorityAddress(userId);
    }

    @Override
    public int updateUserPriorityAddress(String id) {
        return userAddressMapper.updateUserPriorityAddress(id);
    }

    @Override
    public int checkAddressDelivery(String userId, String addressId) {
        UserAddress userAddress = userAddressMapper.selectById(addressId);
        if (userAddress != null) {
            String xzqhCode = userAddress.getXzqhCode();
            XZQHTable xzqhInfo = userAddressMapper.findXZQHInfo(xzqhCode);
            if (xzqhInfo != null) {
                int delivery = xzqhInfo.getDelivery();
                if (delivery == 1) {
                    return 2;
                }
            }
        }
        return 0;
    }
}
